package com.fingard.dsp.bank.directbank.bocom01;

import com.bocom.jump.bp.util.Base64;

import com.fingard.dsp.bank.directbank.bankInterface.IOrderMsg;
import com.fingard.dsp.bank.reqrespobj.ordermsg.ReqOrderMsgQry;
import com.fingard.dsp.bank.reqrespobj.ordermsg.RetAtsOrderMsgQry;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 推送订单信息
 * 
 * @author xtt
 *
 */
public class BOCOM01OrderMsg extends BOCOM01Base implements IOrderMsg {

	@Override
	public RetAtsOrderMsgQry pushOrderMsg(ReqOrderMsgQry p_orderMsg) throws Exception {
		RetAtsOrderMsgQry retAts = new RetAtsOrderMsgQry(p_orderMsg.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
		String tmpRespInfo = "";
		try {
			tr_code = "rcvOrderData";
			req_no = getQryTransID();
			XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
			// 写入通用的头部节点
			writeCommonHeader2(xmlWriter);
			// 写入body
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("ID", getTrmSeqNum());//getTrmSeqNum()
			xmlWriter.writeElementString("SV_CUST_ID_NO", getBankConfig().getValueAsString("svcustidno"));// 主机厂证件号码
			xmlWriter.writeElementString("SV_ID_TYPE", getBankConfig().getValueAsString("svidtype"));// 主机厂证件类型
			xmlWriter.writeElementString("DEALER_CODE",p_orderMsg.DealerCode);// 特约店代码 
			xmlWriter.writeElementString("ORDER_NO",p_orderMsg.OrderCode);// 订单号 
			xmlWriter.writeElementString("DRAFT_NO",p_orderMsg.BillNo);// 票据号 必填
			xmlWriter.writeElementString("TOTAL_PRICE",p_orderMsg.OrderTotallAmount);// 订单金额合计
			xmlWriter.writeElementString("AMOUNT",p_orderMsg.OrderNum);// 申请总数量
			xmlWriter.writeElementString("PRICE",p_orderMsg.PickupPrice);// 申请价格 
			xmlWriter.writeElementString("ACT_TYPE","3");// 处理状态 
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			String reqXml = Base64.encode(xmlWriter.toXmlString().getBytes(getCharset()));
			XmlTextWriter xmlWriters = new XmlTextWriter();// 发银行报文的XML对象
			tr_code = "FHTS01";
			writeCommonHeader(xmlWriters);
			xmlWriters.writeStartElement("body");
			xmlWriters.writeElementClosed("rem");
			xmlWriters.writeElementString("systemId", "SCF");
			xmlWriters.writeElementString("data", reqXml);
			xmlWriters.writeEndDocument();
			String[] tmpStrRet = sendToBank(xmlWriters.toXmlString());// 发送银行
			// String[] tmpStrRet = sendToBank(reqXml);
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				// 解析银行返回
				// base64解码
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String ans_code = tmpBkReader.getFirstText("/ap/head/ans_code");
					String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
					if ("0".equals(ans_code)) {
						String particular_code = tmpBkReader.getFirstText("/ap/head/particular_code");
						String particular_info = tmpBkReader.getFirstText("/ap/head/particular_info");
						if ("0000".equals(particular_code)) {
							String body = new String(Base64.decode(tmpBkReader.getFirstText("/ap/body/RspMsgContent")));
							WriteBankLogStep3(body);
							StringBuilder bodyMsg = new StringBuilder();
							// 解析银行返回
							XmlTextReader bodyBkReader = loadBankRespXmlReader(body, bodyMsg);
							if (bodyBkReader != null) {
								String rsp_status = bodyBkReader.getFirstText("/mes/head/RSP_STATUS");
								String rsp_code = bodyBkReader.getFirstText("/mes/head/RSP_CODE");
								String rsp_msg = bodyBkReader.getFirstText("/mes/head/RSP_MSG");
								if (rsp_status.endsWith("N")) {
										String tmpReturnCode = bodyBkReader.getFirstText("/mes/body/FLAG");
										String tmpReturnMsg = bodyBkReader.getFirstText("/mes/body/BACKUP_FIELD");
										if ("success".equals(tmpReturnCode)) {
											tmpRespCode = "0";
											tmpRespInfo = "处理成功";
										} else {
											tmpRespCode = "-1";
											tmpRespInfo = tmpReturnCode + " : " + tmpReturnMsg;
										}
								} else {
									tmpRespCode = "-1";
									tmpRespInfo = rsp_code + rsp_msg;
								}
							} else {
								tmpRespCode = "-1";
								tmpRespInfo = "返回内容为空";
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = particular_info;
						}
					} else {
						retAts.writeRespCode("-1");
						retAts.writeRespInfo(ans_info);
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = tmpStrRet[1];
			}
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
		retAts.writeEnd();
		return retAts;
	}

}
